/*
 * ProtocolByIdFinder.java
 *
 * Created on March 27, 2007, 11:09 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import org.atomojo.app.db.DB;
import org.infoset.xml.DocumentLoader;
import org.infoset.xml.sax.SAXDocumentLoader;
import org.restlet.Application;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Form;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.resource.Finder;
import org.restlet.resource.ServerResource;

/**
 *
 * @author alex
 */
public class TermFinder extends Finder {

   
   DB atomDB;
   Application theApp;
   Reference resourceBase;
   DocumentLoader loader = new SAXDocumentLoader();
   Storage storage;
   App app;
   

   /** Creates a new instance of ProtocolByIdFinder */
   public TermFinder(Context context,Application theApp,Reference resourceBase,DB atomDB,Storage storage) {
      super(context);
      this.theApp = theApp;
      this.atomDB = atomDB;
      this.resourceBase = resourceBase;
      this.storage = storage;
      this.app = new App(context.getLogger(),atomDB,storage,theApp.getMetadataService());
      setTargetClass(ServerResource.class);
   }
   
   public ServerResource find(Request request, Response response) {

      String uriPath = request.getResourceRef().getRemainingPart();
      /*
      Reference uref = new Reference(uriPath);
      uriPath = uref.getPath();
       */
      if (getContext().getLogger().isLoggable(Level.FINE)) {
         getContext().getLogger().fine("remaining="+uriPath);
         getContext().getLogger().fine("parent="+request.getResourceRef().getParentRef().toString());
         getContext().getLogger().fine("path="+request.getResourceRef().getPath());
      }
      try {
         URI term = null;
         if (uriPath==null || uriPath.length()==0) {
            Form params = request.getResourceRef().getQueryAsForm();
            String termString = params.getFirstValue("u");
            if (termString==null) {
               return new TermListResource(request.getResourceRef(),resourceBase,app);
            }
            term = new URI(termString);
            if (!term.isAbsolute()) {
               response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST,"The term parameter 'u' is not an absolute URI.");
               return null;
            }
         } else {
            int qpos = uriPath.indexOf('?');
            if (qpos>=0) {
               uriPath = uriPath.substring(0,qpos);
            }
            String [] segments = uriPath.split("/");
            if (segments.length==1) {
               term = new URI(Categorization.DEFAULT_SCHEME+segments[0]);
            } else if (segments.length>0 && segments[0].length()>0 && segments[0].charAt(segments[0].length()-1)==':') {
               // we have a full URI
               term = new URI(uriPath);
            } else {
               // assume http
               term = uriPath.startsWith("/") ? new URI("http:/"+uriPath) : new URI("http://"+uriPath);
            }
         }
         return new TermResource((new Reference(request.getResourceRef().getBaseRef().getParentRef().toString()+resourceBase.toString())).normalize(),app,term);
      } catch (URISyntaxException ex) {
         response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST,"Bad term URI: "+ex.getMessage());
         return null;
      }
   }
   
   
}
